/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ep.persistence.controllers;

import ep.persistence.controllers.exceptions.IllegalOrphanException;
import ep.persistence.controllers.exceptions.NonexistentEntityException;
import ep.persistence.entities.T002Doctor;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ep.persistence.entities.T001User;
import ep.persistence.entities.T006Prescription;
import java.util.ArrayList;
import java.util.Collection;
import ep.persistence.entities.T004Insurance;

/**
 *
 * @author Spyros
 */
public class T002DoctorJpaController {

    public T002DoctorJpaController() {
        emf = Persistence.createEntityManagerFactory("EprescPersistencePU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(T002Doctor t002Doctor) {
//        if (t002Doctor.getT006PrescriptionCollection() == null) {
//            t002Doctor.setT006PrescriptionCollection(new ArrayList<T006Prescription>());
//        }
        if (t002Doctor.getT004Insurances() == null) {
            t002Doctor.setT004Insurances(new ArrayList<T004Insurance>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T001User t001User = t002Doctor.getT001User();
            if (t001User != null) {
                t001User = em.getReference(t001User.getClass(), t001User.getId());
                t002Doctor.setT001User(t001User);
            }
//            Collection<T006Prescription> attachedT006PrescriptionCollection = new ArrayList<T006Prescription>();
//            for (T006Prescription t006PrescriptionCollectionT006PrescriptionToAttach : t002Doctor.getT006PrescriptionCollection()) {
//                t006PrescriptionCollectionT006PrescriptionToAttach = em.getReference(t006PrescriptionCollectionT006PrescriptionToAttach.getClass(), t006PrescriptionCollectionT006PrescriptionToAttach.getId());
//                attachedT006PrescriptionCollection.add(t006PrescriptionCollectionT006PrescriptionToAttach);
//            }
//            t002Doctor.setT006PrescriptionCollection(attachedT006PrescriptionCollection);
            Collection<T004Insurance> attachedT004Insurances = new ArrayList<T004Insurance>();
            for (T004Insurance t004InsurancesT004InsuranceToAttach : t002Doctor.getT004Insurances()) {
                t004InsurancesT004InsuranceToAttach = em.getReference(t004InsurancesT004InsuranceToAttach.getClass(), t004InsurancesT004InsuranceToAttach.getId());
                attachedT004Insurances.add(t004InsurancesT004InsuranceToAttach);
            }
            t002Doctor.setT004Insurances(attachedT004Insurances);
            em.persist(t002Doctor);
            if (t001User != null) {
                t001User.getT002DoctorCollection().add(t002Doctor);
                t001User = em.merge(t001User);
            }
//            for (T006Prescription t006PrescriptionCollectionT006Prescription : t002Doctor.getT006PrescriptionCollection()) {
//                T002Doctor oldT002DoctorOfT006PrescriptionCollectionT006Prescription = t006PrescriptionCollectionT006Prescription.getT002Doctor();
//                t006PrescriptionCollectionT006Prescription.setT002Doctor(t002Doctor);
//                t006PrescriptionCollectionT006Prescription = em.merge(t006PrescriptionCollectionT006Prescription);
//                if (oldT002DoctorOfT006PrescriptionCollectionT006Prescription != null) {
//                    oldT002DoctorOfT006PrescriptionCollectionT006Prescription.getT006PrescriptionCollection().remove(t006PrescriptionCollectionT006Prescription);
//                    oldT002DoctorOfT006PrescriptionCollectionT006Prescription = em.merge(oldT002DoctorOfT006PrescriptionCollectionT006Prescription);
//                }
//            }
            for (T004Insurance t004InsurancesT004Insurance : t002Doctor.getT004Insurances()) {
                t004InsurancesT004Insurance.getT002Doctors().add(t002Doctor);
                t004InsurancesT004Insurance = em.merge(t004InsurancesT004Insurance);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(T002Doctor t002Doctor) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T002Doctor persistentT002Doctor = em.find(T002Doctor.class, t002Doctor.getId());
            T001User t001UserOld = persistentT002Doctor.getT001User();
            T001User t001UserNew = t002Doctor.getT001User();
//            Collection<T006Prescription> t006PrescriptionCollectionOld = persistentT002Doctor.getT006PrescriptionCollection();
//            Collection<T006Prescription> t006PrescriptionCollectionNew = t002Doctor.getT006PrescriptionCollection();
            Collection<T004Insurance> t004InsurancesOld = persistentT002Doctor.getT004Insurances();
            Collection<T004Insurance> t004InsurancesNew = t002Doctor.getT004Insurances();
            List<String> illegalOrphanMessages = null;
//            for (T006Prescription t006PrescriptionCollectionOldT006Prescription : t006PrescriptionCollectionOld) {
//                if (!t006PrescriptionCollectionNew.contains(t006PrescriptionCollectionOldT006Prescription)) {
//                    if (illegalOrphanMessages == null) {
//                        illegalOrphanMessages = new ArrayList<String>();
//                    }
//                    illegalOrphanMessages.add("You must retain T006Prescription " + t006PrescriptionCollectionOldT006Prescription + " since its t002Doctor field is not nullable.");
//                }
//            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (t001UserNew != null) {
                t001UserNew = em.getReference(t001UserNew.getClass(), t001UserNew.getId());
                t002Doctor.setT001User(t001UserNew);
            }
//            Collection<T006Prescription> attachedT006PrescriptionCollectionNew = new ArrayList<T006Prescription>();
//            for (T006Prescription t006PrescriptionCollectionNewT006PrescriptionToAttach : t006PrescriptionCollectionNew) {
//                t006PrescriptionCollectionNewT006PrescriptionToAttach = em.getReference(t006PrescriptionCollectionNewT006PrescriptionToAttach.getClass(), t006PrescriptionCollectionNewT006PrescriptionToAttach.getId());
//                attachedT006PrescriptionCollectionNew.add(t006PrescriptionCollectionNewT006PrescriptionToAttach);
//            }
//            t006PrescriptionCollectionNew = attachedT006PrescriptionCollectionNew;
//            t002Doctor.setT006PrescriptionCollection(t006PrescriptionCollectionNew);
            Collection<T004Insurance> attachedT004InsurancesNew = new ArrayList<T004Insurance>();
            for (T004Insurance t004InsurancesNewT004InsuranceToAttach : t004InsurancesNew) {
                t004InsurancesNewT004InsuranceToAttach = em.getReference(t004InsurancesNewT004InsuranceToAttach.getClass(), t004InsurancesNewT004InsuranceToAttach.getId());
                attachedT004InsurancesNew.add(t004InsurancesNewT004InsuranceToAttach);
            }
            t004InsurancesNew = attachedT004InsurancesNew;
            t002Doctor.setT004Insurances(t004InsurancesNew);
            t002Doctor = em.merge(t002Doctor);
            if (t001UserOld != null && !t001UserOld.equals(t001UserNew)) {
                t001UserOld.getT002DoctorCollection().remove(t002Doctor);
                t001UserOld = em.merge(t001UserOld);
            }
            if (t001UserNew != null && !t001UserNew.equals(t001UserOld)) {
                t001UserNew.getT002DoctorCollection().add(t002Doctor);
                t001UserNew = em.merge(t001UserNew);
            }
//            for (T006Prescription t006PrescriptionCollectionNewT006Prescription : t006PrescriptionCollectionNew) {
//                if (!t006PrescriptionCollectionOld.contains(t006PrescriptionCollectionNewT006Prescription)) {
//                    T002Doctor oldT002DoctorOfT006PrescriptionCollectionNewT006Prescription = t006PrescriptionCollectionNewT006Prescription.getT002Doctor();
//                    t006PrescriptionCollectionNewT006Prescription.setT002Doctor(t002Doctor);
//                    t006PrescriptionCollectionNewT006Prescription = em.merge(t006PrescriptionCollectionNewT006Prescription);
//                    if (oldT002DoctorOfT006PrescriptionCollectionNewT006Prescription != null && !oldT002DoctorOfT006PrescriptionCollectionNewT006Prescription.equals(t002Doctor)) {
//                        oldT002DoctorOfT006PrescriptionCollectionNewT006Prescription.getT006PrescriptionCollection().remove(t006PrescriptionCollectionNewT006Prescription);
//                        oldT002DoctorOfT006PrescriptionCollectionNewT006Prescription = em.merge(oldT002DoctorOfT006PrescriptionCollectionNewT006Prescription);
//                    }
//                }
//            }
            for (T004Insurance t004InsurancesOldT004Insurance : t004InsurancesOld) {
                if (!t004InsurancesNew.contains(t004InsurancesOldT004Insurance)) {
                    t004InsurancesOldT004Insurance.getT002Doctors().remove(t002Doctor);
                    t004InsurancesOldT004Insurance = em.merge(t004InsurancesOldT004Insurance);
                }
            }
            for (T004Insurance t004InsurancesNewT004Insurance : t004InsurancesNew) {
                if (!t004InsurancesOld.contains(t004InsurancesNewT004Insurance)) {
                    t004InsurancesNewT004Insurance.getT002Doctors().add(t002Doctor);
                    t004InsurancesNewT004Insurance = em.merge(t004InsurancesNewT004Insurance);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = t002Doctor.getId();
                if (findT002Doctor(id) == null) {
                    throw new NonexistentEntityException("The t002Doctor with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T002Doctor t002Doctor;
            try {
                t002Doctor = em.getReference(T002Doctor.class, id);
                t002Doctor.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The t002Doctor with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
//            Collection<T006Prescription> t006PrescriptionCollectionOrphanCheck = t002Doctor.getT006PrescriptionCollection();
//            for (T006Prescription t006PrescriptionCollectionOrphanCheckT006Prescription : t006PrescriptionCollectionOrphanCheck) {
//                if (illegalOrphanMessages == null) {
//                    illegalOrphanMessages = new ArrayList<String>();
//                }
//                illegalOrphanMessages.add("This T002Doctor (" + t002Doctor + ") cannot be destroyed since the T006Prescription " + t006PrescriptionCollectionOrphanCheckT006Prescription + " in its t006PrescriptionCollection field has a non-nullable t002Doctor field.");
//            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            T001User t001User = t002Doctor.getT001User();
            if (t001User != null) {
                t001User.getT002DoctorCollection().remove(t002Doctor);
                t001User = em.merge(t001User);
            }
            Collection<T004Insurance> t004Insurances = t002Doctor.getT004Insurances();
            for (T004Insurance t004InsurancesT004Insurance : t004Insurances) {
                t004InsurancesT004Insurance.getT002Doctors().remove(t002Doctor);
                t004InsurancesT004Insurance = em.merge(t004InsurancesT004Insurance);
            }
            em.remove(t002Doctor);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<T002Doctor> findT002DoctorEntities() {
        return findT002DoctorEntities(true, -1, -1);
    }

    public List<T002Doctor> findT002DoctorEntities(int maxResults, int firstResult) {
        return findT002DoctorEntities(false, maxResults, firstResult);
    }

    private List<T002Doctor> findT002DoctorEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(T002Doctor.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public T002Doctor findT002Doctor(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(T002Doctor.class, id);
        } finally {
            em.close();
        }
    }

    public int getT002DoctorCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<T002Doctor> rt = cq.from(T002Doctor.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
